<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑会员</title>
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css">
    <style>
        .form-content {
            padding: 20px;
        }
    </style>
</head>
<body>
<div class="form-content">
    <form class="layui-form" lay-filter="memberForm">
        <input type="hidden" name="id" value="<?= $member->id ?>">
        
        <div class="layui-form-item">
            <label class="layui-form-label">会员卡号</label>
            <div class="layui-input-block">
                <input type="text" name="card_no" value="<?= $member->card_no ?>" class="layui-input" readonly>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">会员姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" value="<?= $member->name ?>" required lay-verify="required" placeholder="请输入会员姓名" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">手机号码</label>
            <div class="layui-input-block">
                <input type="text" name="phone" value="<?= $member->phone ?>" required lay-verify="required|phone" placeholder="请输入手机号码" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">会员等级</label>
            <div class="layui-input-block">
                <select name="level" required lay-verify="required">
                    <option value="">请选择会员等级</option>
                    <?php foreach($levels as $level): ?>
                    <option value="<?= $level->id ?>" <?= $member->level == $level->id ? 'selected' : '' ?>><?= $level->name ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">所属门店</label>
            <div class="layui-input-block">
                <select name="store_id" required lay-verify="required">
                    <option value="">请选择所属门店</option>
                    <?php foreach($stores as $store): ?>
                    <option value="<?= $store->id ?>" <?= $member->store_id == $store->id ? 'selected' : '' ?>><?= $store->name ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">会员状态</label>
            <div class="layui-input-block">
                <select name="status" lay-verify="required" lay-filter="memberStatus">
                    <option value="1" <?= $member->status == 1 ? 'selected' : '' ?>>正常</option>
                    <option value="2" <?= $member->status == 2 ? 'selected' : '' ?>>冻结</option>
                    <?php if((int)session('user.role') !== 4): ?>
                    <option value="3" <?= $member->status == 3 ? 'selected' : '' ?>>注销</option>
                    <?php endif; ?>
                </select>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">生日</label>
            <div class="layui-input-block">
                <input type="text" name="birthday" id="birthday" value="<?= $member->birthday ?>" placeholder="请选择生日" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" value="<?= $member->address ?>" placeholder="请输入地址" class="layui-input">
            </div>
        </div>
        
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"><?= $member->remark ?></textarea>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="memberForm">立即提交</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="closeLayer()">关闭</button>
            </div>
        </div>
    </form>
</div>

<script src="/app/admin/component/layui/layui.js"></script>
<script>
layui.use(['form', 'layer', 'laydate'], function(){
    var $ = layui.$
    ,form = layui.form
    ,layer = layui.layer
    ,laydate = layui.laydate;
    
    // 生日选择
    laydate.render({
        elem: '#birthday'
    });
    
    // 监听状态选择
    form.on('select(memberStatus)', function(data){
        if(data.value == '3'){ // 选择注销状态
            layer.confirm('注意：会员注销后将无法恢复，会员卡将被禁用，确定要注销该会员吗？', {
                btn: ['确定','取消'],
                title: '注销确认',
                skin: 'layui-layer-warning'
            }, function(){
                // 用户点击确定，保持选择注销状态
                layer.closeAll();
            }, function(){
                // 用户点击取消，恢复为原来的状态
                var originalStatus = '<?php echo $member->status; ?>';
                $('select[name="status"]').val(originalStatus);
                form.render('select');
            });
        }
    });
    
    // 监听提交
    form.on('submit(memberForm)', function(data){
        var field = data.field;
        
        // 如果是注销状态，再次确认
        if(field.status == '3'){
            layer.confirm('再次确认：是否注销该会员？注销后将无法恢复！', {
                btn: ['确定注销','取消'],
                title: '最终确认',
                skin: 'layui-layer-danger'
            }, function(){
                // 确认注销，提交表单
                submitForm(field);
            });
            return false;
        }
        
        // 非注销状态，直接提交
        submitForm(field);
        return false;
    });
    
    // 提取表单提交函数
    function submitForm(field) {
        $.ajax({
            url: '/app/ky_shouyin/admin/member/edit'
            ,type: 'POST'
            ,data: field
            ,success: function(res){
                if(res.code === 0){
                    layer.msg('编辑成功', {
                        icon: 1
                        ,time: 1000
                    }, function(){
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.layui.table.reload('LAY-member-list');
                    });
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }
        });
    }
    
    window.closeLayer = function(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
});
</script>
</body>
</html> 